package com.baor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baor.dao.MenuDao;
import com.baor.dao.RoleDao;
import com.baor.domain.RoleVo;
import com.baor.service.RoleService;
import com.baor.utils.AjaxResult;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.relation.Role;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private MenuDao menuDao;

    @Override
    public AjaxResult<?> roleList(int pageIndex, int pageSize) {
        Page<RoleVo> page = new Page<>();
        page.setCurrent(pageIndex);
        page.setSize(pageSize);
        LambdaQueryWrapper<RoleVo> lqw = new LambdaQueryWrapper<>();
        lqw.notIn(RoleVo::getRoleId, 1)
                .select(RoleVo.class,x -> !x.getColumn().equals("menu_role") && !x.getColumn().equals("role_superiors"));
        Page<RoleVo> page1 = roleDao.selectPage(page, lqw);
        Map<String,Object> map = new HashMap<>();
        map.put("roleList",page1.getRecords());
        map.put("pageCount", page1.getPages());
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult<?> searchRole(Map<String,Object> param) {
        int pageIndex = (int) param.get("pageIndex");
        int pageSize = (int) param.get("pageSize");
        Page<RoleVo> page = new Page<>(pageIndex,pageSize);
        LambdaQueryWrapper<RoleVo> lqw = new LambdaQueryWrapper<>();
        lqw.like(RoleVo::getRoleName, param.get("roleName"))
                .notIn(RoleVo::getRoleId, 1)
                .select(RoleVo.class,x -> !x.getColumn().equals("menu_role") && !x.getColumn().equals("role_superiors"));
        Page<RoleVo> page1 = roleDao.selectPage(page,lqw);
        Map<String,Object> map = new HashMap<>();
        map.put("roleList",page1.getRecords());
        map.put("pageCount", page1.getPages());
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult<?> delete(int roleId) {
        int row = roleDao.deleteById(roleId);
        if(row > 0){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    @Override
    public AjaxResult<?> insertRole(Map<String,Object> params) {
        int row = roleDao.addRole(params);
        if(row > 0){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    @Override
    public AjaxResult<?> highterRole() {
        return AjaxResult.success(roleDao.higherRole());
    }
}
